package com.thiscc.sum.dao.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.thiscc.sum.common.MongoDBManager;
import com.thiscc.sum.dao.CommonDao;

@Component
public class CommonDaoImpl implements CommonDao {
    
    private static Log log = LogFactory.getLog(CommonDaoImpl.class);
    
    /*-------------------------------------------------------------------------------------------*/

    public List<DBObject> searchCommon(String collection, DBObject conditionBean) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.find(collection, conditionBean);
    }
    
    /*-------------------------------------------------------------------------------------------*/
    public List<DBObject> searchCommon(String collection, DBObject conditionBran, DBObject orderBy) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.find(collection, conditionBran, orderBy);
    }
    
    /*-------------------------------------------------------------------------------------------*/
    public DBObject searchLessCommon(String collection, DBObject conditionBran) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.findOne(collection, conditionBran);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public DBObject searchLessCommonOne(String collection, DBObject conditionBran, DBObject less) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.findOne(collection, conditionBran, less);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public DBObject searchCommonOne(String collection, DBObject conditionBean) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.findOne(collection, conditionBean);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public boolean insertCommon(String collection, DBObject insertBean) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        boolean flg = false;
        
        try {
            WriteResult result = db.insert(collection, insertBean);
            if (null != result) {
                
                if (null == result.getError() || "null".equals(result.getError())) {
                    flg = true;
                }
            }
        }
        catch (Exception e) {
        }
        
        return flg;
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public boolean updateCommon(String collection, DBObject conditionBean, DBObject updateBean) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        boolean flg = false;
        
        try {
            flg = db.update(collection, conditionBean, updateBean);
        }
        catch (Exception e) {
        }
        
        return flg;
    }
    
    public boolean updateInt(String collection, DBObject conditionBean, DBObject updateBean) {
    	
    	MongoDBManager db = MongoDBManager.getInstance();
        
        boolean flg = false;
        
        try {
            flg = db.updateInt(collection, conditionBean, updateBean);
        }
        catch (Exception e) {
        }
        
        return flg;
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public List<DBObject> searchCommonAll(String collection) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.findAll(collection);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public List<DBObject> searchLessCommonAll(String collection, DBObject less) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        DBObject conditionBean = new BasicDBObject();
        
        return db.findLess(collection, conditionBean, less);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public boolean insertBatchCommon(String collection, List<DBObject> list) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        try {
            db.insertBatch(collection, list);
            return true;
        }
        catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
        }
        return false;
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public long getCount(String collection) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.getCount(collection);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public List<DBObject> searchLessCommon(String collection, DBObject conditionBean, DBObject orderByBean) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.findLess(collection, conditionBean, orderByBean);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public boolean updateCommonPush(String collection, DBObject conditionBean, DBObject updateBean) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        boolean flg = true;
        
        try {
            db.updatePush(collection, conditionBean, updateBean);
        }
        catch (Exception e) {
            flg = false;
        }
        
        return flg;
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public boolean deleteCommon(String collection, DBObject condition) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        boolean flg = false;
        try {
            flg = db.delete(collection, condition);
        }
        catch (Exception e) {
        }
        
        return flg;
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public boolean updatePull(String collection, DBObject condition, DBObject delete) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        boolean flg = true;
        try {
            
            db.updatePull(collection, condition, delete);
        }
        catch (Exception e) {
            flg = false;
        }
        
        return flg;
    }
    
    /*-------------------------------------------------------------------------------------------*/
    // 这个类报错
    
    public JSONArray mapReduceCommon(String collection, String map, String reduce, DBObject q) {
        MongoDBManager db = MongoDBManager.getInstance();
        
        return db.mapReduce(collection, map, reduce, q);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public List<DBObject> searchLessCommon(String collection, DBObject conditionBean, int pageNo, int perPageCount,
        DBObject orderBy) {
        MongoDBManager db = MongoDBManager.getInstance();
        return db.findLess(collection, conditionBean, pageNo, perPageCount, orderBy);
        
    }
    
    public List<DBObject> searchLessCommon(String collection, DBObject conditionBean, DBObject less, DBObject orderBy,
        int pageNo, int perPageCount) {
        MongoDBManager db = MongoDBManager.getInstance();
        return db.findLess(collection, conditionBean, less, orderBy, pageNo, perPageCount);
        
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public long getCommonCount(String collection, DBObject conditionBean) {
        MongoDBManager db = MongoDBManager.getInstance();
        return db.getCount(collection, conditionBean);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public List<DBObject> searchLessCommon(String collection, DBObject conditionBean, DBObject filedsBean,
        DBObject orderBy) {
        MongoDBManager db = MongoDBManager.getInstance();
        return db.findLess(collection, conditionBean, filedsBean, orderBy);
    }
    
    /*-------------------------------------------------------------------------------------------*/

    public boolean updatePopCommon(String collection, DBObject conditionBean, DBObject updatePopBean) {
        MongoDBManager db = MongoDBManager.getInstance();
        return db.updatePop(collection, conditionBean, updatePopBean);
    }
    
    public boolean updateCommonPull(String collection, DBObject conditionBean, DBObject updateBean) {
        boolean successFlag = false;
        try {
            MongoDBManager.getInstance().updatePull(collection, conditionBean, updateBean);
            successFlag = true;
        }
        catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
        }
        return successFlag;
    }
    
    public Object searchCommonPage(String collection, DBObject conditionBean, DBObject orderBy, int pageNo,
        int perPageCount) {
        
        MongoDBManager db = MongoDBManager.getInstance();
        
        // 分页查询数据
        List<DBObject> obj = db.findPage(collection, conditionBean, orderBy, pageNo, perPageCount);
        
        // 总条数
        long count = db.getCount(collection, conditionBean);
        
        JSONObject jo = new JSONObject();
        jo.put("data", obj.toString());
        jo.put("total", count);
        
        return jo;
    }
    
}
